import './axios';

import type { App } from 'vue';

import useAuthStore from '@/stores/auth.store';

export default {
  install: (vm: App) => {
    vm.directive('permission', {
      mounted(el, binding) {
        const authStore = useAuthStore();
        const { value } = binding;
        const permissions = value.split(',');
        if (
          ['18181818181'].includes(authStore.account)
          && permissions.includes('ROOT')
        ) return;
        if (!permissions.some((permission: string) => authStore.permissions.includes(permission))) {
          el.remove();
        }
      },
    });
    vm.directive('role', {
      mounted(el, binding) {
        const authStore = useAuthStore();
        const { value } = binding;
        const roles = value.split(',');
        if (
          ['18181818181'].includes(authStore.account)
          && roles.includes('ROOT')
        ) return;
        if (!roles.some((role: string) => authStore.roles.includes(role))) {
          el.remove();
        }
      },
    });
  },
};
